home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2002 November / SGI IRIX 6.5 Applications 2002 November.iso / dist / gateway.idb / usr / WebFace / Source / 10-InfoSystems / samba / samba-share.frm.z / samba-share.frm
Encoding:
Text File  |  2002-06-12  |  10.4 KB  |  390 lines

  1. #!/usr/bin/perl5
  2. #
  3. # samba-share.cgi
  4. #
  5. # Copyright 1988-1996 Silicon Graphics, Inc.
  6. # All rights reserved.
  7. #
  8. # This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  9. # the contents of this file may not be disclosed to third parties, copied or
  10. # duplicated in any form, in whole or in part, without the prior written
  11. # permission of Silicon Graphics, Inc.
  12. #
  13. # RESTRICTED RIGHTS LEGEND:
  14. # Use, duplication or disclosure by the Government is subject to restrictions
  15. # as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  16. # and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  17. # successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  18. # rights reserved under the Copyright Laws of the United States.
  19. #
  20. # $Id: samba-share.frm,v 1.10 1998/02/11 23:21:46 jrw Exp $
  21.  
  22. require "/usr/OnRamp/lib/OnRamp.pm";
  23. require "/usr/OnRamp/lib/java.pm";
  24.  
  25. $myname = "samba-share.cgi";
  26. $help_page = "samba-share-help.html";
  27. $title = "SAMBA Shares";
  28.  
  29. $js_main = "
  30. type = \"share\";
  31. $js_account_main;
  32. $js_error_box;
  33. $js_help
  34. $js_filename;
  35. function testAdd(form) {
  36.     if (form.new_account.value.length == 0) {
  37.         errorBox(form.new_account, \"To create a new share, enter\\n\"
  38.             + \"the new share name and click\\n\"
  39.             + \"the add button.\");
  40.         return (false);
  41.     }
  42.     if (!testSambaName(form.new_account)) return (false);
  43.     return (true);
  44. }
  45. function testSambaName(Ctrl) {
  46.     if (!testFileChars(Ctrl.value)) {
  47.         errorBox(Ctrl, \"The share name cannot contain meta \"
  48.             + \"characters.\");
  49.         return (false);return (false);
  50.     }
  51.     word = Ctrl.value;
  52.     if (word.indexOf(' ', 0) != -1 || word.indexOf('\t', 0) != -1) {
  53.         errorBox(Ctrl, \"The share name cannot contain whitespace \"
  54.                 + \"characters.\");
  55.         return (false);
  56.     }
  57.     return (true);
  58. }";
  59.  
  60. $js_edit = 
  61. "$js_standard;
  62. $js_error_box;
  63. $js_help
  64. $js_filename;
  65. function checkForm(form) {
  66.     if (form.new_name && !testSambaName(form.new_name)) return (false);
  67.     if (form.comment.value.length > 0 && !testFileChars(form.comment.value)) {
  68.         errorBox(form.comment, \"The comment cannot contain \"
  69.             + \"meta characters.\");
  70.         return (false);
  71.     }
  72.     if (form.path.value.length == 0) return (true);
  73.     if (!testFilename(form.path, \"path\")) return (false);
  74.     return (true);
  75. }
  76. function testSambaName(Ctrl) {
  77.     if (!testFileChars(Ctrl.value)) {
  78.         errorBox(Ctrl, \"The share name cannot contain meta \"
  79.             + \"characters.\");
  80.         return (false);
  81.     }
  82.     word = Ctrl.value;
  83.     if (word.indexOf(' ', 0) != -1 || word.indexOf('\t', 0) != -1) {
  84.         errorBox(Ctrl, \"The share name cannot contain whitespace \"
  85.                 + \"characters.\");
  86.         return (false);
  87.     }
  88.     return (true);
  89. }";
  90.  
  91.  
  92. print "Content-type: text/html\n\n";
  93.  
  94. if ( -e "/usr/freeware/lib/samba/smb.conf" ) {
  95.     $conf_dir = "/usr/freeware/lib/samba";
  96. } elsif ( -e "/usr/local/samba/lib/smb.conf" ) {
  97.     $conf_dir = "/usr/local/samba/lib";
  98. } elsif ( -e "/usr/samba/lib/smb.conf" ) {
  99.     $conf_dir = "/usr/samba/lib";
  100. } else {
  101.     &title_block($title);
  102.     &header_block($title);
  103.     print "<i>SAMBA software not installed.  Please install subsystem "
  104.         . "</i><b>fw_samba</b><i> from the <a "
  105.         . "href=\"http://www.sgi.com/TasteOfDT/public/freeware.html\" "
  106.         . "target=\"FREEWARE\">SGI freeware CD</a> or download the "
  107.     . "software from the Internet.</i>\n\n";
  108.     exit 0;
  109. }
  110. $conf = "$conf_dir/smb.conf";
  111. $dummy = "$conf_dir/smb.conf.tmp";
  112.  
  113. &get_fields;
  114.  
  115. &getAccounts;
  116.  
  117. if (%fld) {
  118.         $fld{'chosen'} =~ /([\w.-]+)/;
  119.     $fld{'chosen'} = $1;
  120.  
  121.     $help = $document_root . $ENV{"SCRIPT_NAME"};
  122.     $help =~ s/cgi$/hlp/;
  123.     exec $help if ($fld{'help'} eq "Help");
  124.  
  125.     if ($fld{'add'}) {
  126.             &error(2,"Share name required.") if !$fld{'new_account'};
  127.             &error(2,"Invalid share name.") 
  128.             if $fld{'new_account'} =~ /$METACHARS/o || $fld{'new_account'} =~ /\s/; 
  129.             $fld{'chosen'} = $fld{'new_account'}; 
  130.         $fld{'mode'} = "add";
  131.         $val{'public'} = "No";
  132.         $val{'writeable'} = "No";
  133.         $val{'printable'} = "No";
  134.  
  135.             &editAccount; 
  136.     }
  137.         elsif ($fld{'delete'}) {
  138.             &error(2,"To delete an existing share, first select a share from "
  139.                     . "list, then click the delete button.") if !$fld{'chosen'};
  140.         $message = qq|Click "Ok" to save changes.|;
  141.         &generic($fld{'chosen'}); 
  142.     }
  143.     elsif ($fld{'edit'}) {
  144.         &error(2,"To edit an existing share, first select a share from "
  145.             . "list, then click the edit button.") if !$fld{'chosen'};
  146.         &get_vals($fld{'chosen'});
  147.         $fld{'mode'} = "edit";
  148.         &editAccount; 
  149.     }
  150.     elsif ($fld{'doedit'}) { 
  151.         if ($fld{'mode'} eq "add") { &doAdd; }
  152.         else { &doEdit; } 
  153.         &restart;
  154.         &getAccounts; 
  155.         &generic; 
  156.     }
  157.         elsif ($fld{'doit'}) { &tryToDelete; &restart; &getAccounts; &generic; }
  158.         else { $message = "Use buttons to submit form."; $val{'new_account'} = $fld{'new_account'}; &generic; }
  159. } else { &generic; }
  160.  
  161. sub error {
  162.     &error_block($_[1]);
  163.     %val = %fld;
  164.     if ($_[0] == 0) { &addAccount; }
  165.     elsif ($_[0] == 1) { &editAccount; }
  166.     else { &generic; }
  167.     exit 0;
  168. }
  169.  
  170. sub restart {
  171.     $chk = system ("/etc/chkconfig", "samba");
  172.     if (! $chk) {
  173.         system("/etc/init.d/samba stop > /dev/null 2>&1");
  174.         system("/etc/init.d/samba start > /dev/null 2>&1");
  175.     }
  176. }
  177.  
  178. sub doEdit {
  179.        open(IN, "< $conf");
  180.     open(OUT, "> $dummy");
  181.  
  182.     while(<IN>) {
  183.         if ($_ =~ /\[$fld{'chosen'}\]/) { 
  184.             print OUT "[" . $fld{'new_name'} . "]\n";
  185.             
  186.             &print_share;
  187.  
  188.             $done = 0;
  189.             while(!$done) {
  190.                 $in = <IN>;
  191.                 if ($in eq "" || $in =~ /\[.*\]/) {
  192.                     $done = 1;
  193.                     print OUT $in;
  194.                 }
  195.             }
  196.         } else { print OUT $_; }
  197.     }
  198.     close(IN);
  199.     close(OUT);
  200.     rename($dummy, $conf);
  201.      
  202.     $message = "Share edited.";
  203. }    
  204.  
  205. sub print_share {
  206.     if ($fld{'comment'}) { print OUT "   comment = $fld{'comment'}\n"; }
  207.     if ($fld{'path'}) { print OUT "   path = $fld{'path'}\n"; }
  208.     if ($fld{'public'} eq "Yes") { print OUT "   public = yes\n"; }
  209.     if ($fld{'writeable'} eq "Yes") { print OUT "   writeable = yes\n"; }
  210.     if ($fld{'printable'} eq "Yes") { print OUT "   printable = yes\n"; }
  211.     print OUT "\n"; 
  212. }
  213.  
  214. sub get_vals {
  215.     $val{'public'} = "No";
  216.     $val{'writeable'} = "No";
  217.     $val{'printable'} = "No";
  218.  
  219.     open(IN, "< $conf");
  220.     $read_lines = 0;
  221.     while(<IN>) {
  222.         if ($_ =~ /\[$fld{'chosen'}\]/) { $read_lines = 1; next; }
  223.         elsif ($_ =~ /\[.*\]/) { $read_lines = 0; next; }
  224.         next if $read_lines == 0;
  225.         next if substr($_,0,1) eq ";";
  226.     
  227.         if ($_ =~ /(.*)=(.*)/) {
  228.             $parm = $1;
  229.             $value = $2;    
  230.  
  231.             $parm =~ /^\s*([^\s].*[^\s])\s*$/; $parm = $1;
  232.             $value =~ /^\s*([^\s].*)$/; $value = $1;
  233.  
  234.             if ($parm eq "public" && $value eq "yes") { $val{'public'} = "Yes"; }
  235.             if ($parm eq "writeable" && $value eq "yes") { $val{'writeable'} = "Yes"; }
  236.             if ($parm eq "read only" && $value eq "no") { $val{'writeable'} = "Yes"; }
  237.             if ($parm eq "printable" && $value eq "yes") { $val{'printable'} = "Yes"; }
  238.  
  239.             if ($parm eq "comment" || $parm eq "path") {
  240.                 $val{$parm} = $value;
  241.             }
  242.         }
  243.     }
  244. }
  245.  
  246. sub editAccount {
  247.         &js_title_block($title,$js_edit);
  248.  
  249.         &header_block("Edit SAMBA Share");
  250.  
  251.     print "$message<br>";
  252.         
  253.     print "<form name=StandardForm action=$myname method=post onSubmit=\"return runSubmit()\">";
  254.  
  255.     print "<input type=hidden name='chosen' value=$fld{'chosen'}>\n"
  256.         . "<input type=hidden name='mode' value=$fld{'mode'}>\n";
  257.  
  258.     print "<center><table width=450>\n";
  259.  
  260.     if ($fld{'mode'} eq "edit") {
  261.         print "<tr><th align=left>Share name:"
  262.             . "<td>", &text("new_name", $fld{'chosen'}, 20), "</td></tr>\n";
  263.     } else {
  264.         print "<tr><th align=left>Share name:"
  265.             . "<td><tt>$fld{'chosen'}</tt></td></tr>\n";
  266.     }
  267.     
  268.     print "<tr><th align=left>Comment for share:"
  269.         . "<td>", &text("comment", $val{'comment'}, 20), "</td></tr>\n";
  270.  
  271.     print "<tr><th align=left>Mount path of share:"
  272.         . "<td>", &text("path", $val{'path'}, 20), "</td></tr>\n";
  273.  
  274.     print "<tr><th align=left>Allow public access?"
  275.         . "<td><b>", &radio("public", $val{'public'}, "Yes", "No"), "</b></td></tr>\n";
  276.  
  277.     print "<tr><th align=left>Allow write privileges?"
  278.         . "<td><b>", &radio("writeable", $val{'writeable'}, "Yes", "No"), "</b></td></tr>\n";
  279.  
  280.     print "<tr><th align=left>Is this a printing service share?"
  281.         . "<td><b>", &radio("printable", $val{'printable'}, "Yes", "No"), "</b></td></tr>\n";
  282.  
  283.     print "</table></center><br>\n";
  284.  
  285.         print &js_buttons('doedit','Ok','onClick="markOK()"',
  286.         'onClick="markOther()"',
  287.         "onClick=\"do_help('$help_page'); return (false)\"");
  288.  
  289.         print "</form></body></html>";
  290. }
  291.     
  292. sub tryToDelete {
  293.     if ($fld{'del_name'} eq "") { return; }
  294.  
  295.     open(IN, "< $conf");
  296.     open(OUT, "> $dummy");
  297.     while(<IN>) {
  298.         if ($_ =~ /\[$fld{'del_name'}\]/) {
  299.             while(<IN>) {
  300.                 if ($_ =~ /\[.*\]/) { print OUT $_; last; }
  301.             }
  302.         } else { print OUT $_; }
  303.     }
  304.     close(IN);
  305.     close(OUT);
  306.     rename($dummy, $conf);
  307.  
  308.     $message = "Share deleted.";    
  309. }
  310.  
  311. sub doAdd {
  312.     open(OUT, ">> $conf");
  313.  
  314.     print OUT "\n[" . $fld{'chosen'} . "]\n";
  315.     &print_share;
  316.         
  317.     close(OUT);
  318.  
  319.     $message .= "Share added.";
  320. }
  321.  
  322. sub getAccounts {
  323.         $i = 0;
  324.         open(IN,"< $conf");
  325.         while(<IN>) {
  326.             if ($_ =~ /\[(.*)\]/) {
  327.             next if $1 eq "global";
  328.                 $name[$i++] = $1;
  329.             }
  330.         }
  331.         $number = $i;
  332.         close(IN);
  333. }
  334.  
  335. sub generic {
  336.     &js_title_block($title,$js_main);
  337.     &header_block($title);
  338.  
  339.     if (!$number && !$message) { $message = "No existing SAMBA shares."; }
  340.     print "<i>$message</i>";
  341.  
  342.     print "<form name=AccountForm action=$myname method=post onSubmit=\"return runSubmit()\">";
  343.     
  344.     if ($_[0]) { print qq|<input type=hidden name="del_name" value=$_[0]>|; }
  345.  
  346.     if ($number) {
  347.     print "<center><table width=400>";
  348.  
  349.     print qq|<tr><td><input type=submit name="add" value="Add New Share"|,
  350.         qq| onClick="markAdd()"> |,
  351.             qq|</td><td><input name="new_account" value="$val{'new_account'}" size=19></td></tr>|;
  352.  
  353.     print "<tr><td>";
  354.     print qq|<input type=submit name="edit" 
  355.         value="Edit Selected Share" onClick="markEdit()"></td>|;
  356.  
  357.     undef @locList;
  358.     for ($i=0;$i<$number;$i++) { 
  359.         if ($_[0] ne $name[$i]) { push(@locList,$name[$i]); }
  360.     }
  361.  
  362.     print "<td rowspan=2>";
  363.  
  364.     print &choice_list(*locList,"chosen",20);
  365.  
  366.     print "</td></tr>";
  367.  
  368.     print qq|<tr><td><input type=submit name="delete" 
  369.         value="Delete Selected Share" onClick="markDelete()"></td></tr>|;    
  370.  
  371.  
  372.     print "</table></center><br>";
  373.     } else {
  374.     print "<center><table width=400>";
  375.  
  376.     print qq|<tr><td><input type=submit name="add" value="Add New Share" |,
  377.     qq|onClick="markAdd()"> |,
  378.            qq|</td><td><input name="new_account" value="$val{'new_account'}" size=19></td></tr>|;
  379.  
  380.     print "<tr><td>";
  381.     print "</table>";
  382.     }
  383.  
  384.     print &js_buttons('doit','Ok','onClick="markOther()"',
  385.         'onClick="markOther()"',
  386.         "onClick=\"do_help('$help_page'); return (false)\"");
  387.  
  388.     print "</form></body></html>";
  389. }
  390.